package cn.xz.zysjz.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.WindowManager;

import java.util.Objects;

import cn.xz.zysjz.R;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.databinding.ActivityPhoneBinding;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.SessionUtils;
import cn.xz.zysjz.utils.ToastUtils;


/**
 * 手机号界面*/
public class PhoneActivity extends AppCompatActivity {

    ActivityPhoneBinding mBinding;

    private Boolean canSend = true; //能否绑定

    private CountDownTimer mDownTimer;

    private String mCode = "123456"; //验证码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_phone);
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        initEvent();
        initData();
    }

    public void initData(){
        //当前的手机号
        String mPhone = SessionUtils.getString(CommonLib.USER_PHONE, "");
        if (!Objects.equals(mPhone, "")){
            mBinding.tvBd.setText("当前绑定的手机号: " + mPhone);
        }else {
            mBinding.tvBd.setText("当前未绑定手机号");
        }
    }

    public void initEvent(){
        //返回
        mBinding.viewBack.setOnClickListener(v->{
            finish();
        });
        //点击发送验证码
        mBinding.tvSendY.setOnClickListener(v->{
            //手机号
            String mPhone = mBinding.etPhone.getText().toString();
            if (!AppHelper.isMobileNO(mPhone)){
                ToastUtils.show("请输入正确的手机号");
                return;
            }
            if (canSend){
                //开启60秒倒计时
                canSend = false;
                startTime();
            }
        });
        //点击绑定
        mBinding.tvYy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //手机号
                String mPhone = mBinding.etPhone.getText().toString();
                //验证码
                String mCodes = mBinding.etMsg.getText().toString();
                if (mPhone.isEmpty()){
                    ToastUtils.show("手机号不能为空");
                    return;
                }
                if (!AppHelper.isMobileNO(mPhone)){
                    ToastUtils.show("请输入正确的手机号");
                    return;
                }
                if (mCodes.isEmpty()){
                    ToastUtils.show("验证码不能为空");
                    return;
                }
                if (mCodes.equals(mCode)){
                    ToastUtils.show("绑定成功");
                    SessionUtils.commitString(CommonLib.USER_PHONE, mPhone);
                    mBinding.etPhone.setText("");
                    mBinding.etMsg.setText("");
                    initData();
                }else {
                    ToastUtils.show("验证码错误");
                }
            }
        });
    }

    private void startTime(){
        mDownTimer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                String mT = (millisUntilFinished/1000) + "";
                mBinding.tvSendY.setText( mT + "秒后重发");
            }

            @Override
            public void onFinish() {
                cancelTime();
            }
        }.start();
    }

    private void cancelTime(){
        canSend = true;
        mBinding.tvSendY.setText("发送验证码");
        if (mDownTimer != null){
            mDownTimer.cancel();
            mDownTimer = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelTime();
    }
}